
package com.v.air.app;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 
 * @brief 基本类 所有Activity需继承此类
 * @author Fanhao.Yi
 * @date 2015年4月27日上午11:09:44
 * @version V1.0
 */
public abstract class BaseActivity extends Activity implements IBaseActivity {

	/** 整个应用Applicaiton **/
	protected AppContext myApplication = null;
	/** 当前Activity的弱引用，防止内存泄露 **/
	private WeakReference<Activity> context = null;
	/** 当前Activity渲染的视图View **/
	private View mContextView = null;
	/** 日志输出标志 **/
	protected final String TAG = this.getClass().getSimpleName();

	protected TextView action_title;
	protected RelativeLayout layout_action;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContextView = LayoutInflater.from(this).inflate(bindLayout(), null);
		setContentView(mContextView);
		AppManager.getAppManager().addActivity(this);

		initParam();
		// 初始化控件
		initView(mContextView);
		// 业务操作
		initData(this);
		initListener();
	}

	private void initParam() {
		// 获取应用Application
		myApplication = (AppContext) getApplicationContext();
		// 将当前Activity压入栈
		context = new WeakReference<Activity>(this);
	}

	public void initHead(View view) {
		layout_action = (RelativeLayout) view.findViewById(R.id.layout_action);
		action_title = (TextView) view.findViewById(R.id.action_title);
	}

	/**
	 * 自定义findViewById
	 * 
	 * @param id
	 *            控件资源ID
	 * @return
	 */
	public <T extends View> T $(int id) {
		return (T) super.findViewById(id);
	}

	/**
	 * 获取当前Activity
	 * 
	 * @return 当前Activityd对象
	 */

	protected Activity getContext() {
		if (null != context)
			return context.get();
		else
			return null;
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(TAG, "BaseActivity-->onRestart()");
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "BaseActivity-->onStart()");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "BaseActivity-->onResume()");
		resume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "BaseActivity-->onPause()");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "BaseActivity-->onStop()");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "BaseActivity-->onDestroy()");
		destroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AppManager.getAppManager().finishActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/*public void onBackClicked(View view) {
		if (view.getId() == R.id.action_back) {
			AppManager.getAppManager().finishActivity();
			PageAnimationUtil.downd(this);
		}
	}*/
}
